package com.nation.controller;

import com.nation.domain.*;
import com.nation.service.DevelopmentService;
import com.nation.service.SocietyService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/3.
 */
@Controller
@RequestMapping(value="/Society")
public class SocietyController {
    @Autowired
    SocietyService SocietyService;

    /**
     * 社会状况
     * @param map
     * @return
     */
    @RequestMapping(value = "/society")
    public String development(ModelMap map){
        List<VSocialAndCondition> slist=SocietyService.findAll();
        map.put("slist",slist);
        return "Society/society";
    }

    /**
     * 修改页面
     * @param country_code
     * @param map
     * @return
     */
    @RequestMapping(value = "/society_update")
    public String development_update(String country_code,ModelMap map){
        VSocialAndCondition society=SocietyService.findByCountryCode(country_code);
        map.put("society",society);
        return "Society/society-update";
    }

    /**
     * 修改信息
     * @param VSocialAndCondition
     * @return
     */
    @RequestMapping(value = "/society_update.do")
    @ResponseBody
    public String development_pupdate_do(VSocialAndCondition VSocialAndCondition){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VSocialAndCondition.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=SocietyService.update(VSocialAndCondition);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/society_add")
    public String society_add(){
        return "Society/society-add";
    }
    @RequestMapping(value="/society_add.do")
    @ResponseBody
    public String society_add_do(HttpServletRequest request,@RequestParam(value = "picture",required = false)MultipartFile[] pictures,
                                 String statistics, String conferences, Long population, int census_time, String nation, String marriage_system,
                                 String religion, String festival, String country_code, String people_livelihood ,
                                 String reference , String news_publish){

        VSocialAndCondition VSocialAndCondition=new VSocialAndCondition(statistics,conferences,population,census_time,nation,marriage_system,
                religion,festival,country_code,people_livelihood
                ,reference ,news_publish);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        VSocialAndCondition.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VSocialAndCondition.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge = null;
        try{
//            System.out.println("picture   lenth="+pictures.length);
            // 判断是否有文件上传
            if (pictures!=null&&pictures.length>0) {//有文件上传
                System.out.println("picture0000");
                System.out.println("picture   lenth="+pictures.length);
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.SOCIETY;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.SOCIETY));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VSocialAndCondition.setPicture(picture_path);
            }

            judge=SocietyService.insert(VSocialAndCondition);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败，出错";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request, ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VSocialAndCondition> elist=SocietyService.findByCondition(map);
        mmap.put("ilist",elist);
        return "Society/society";
    }
    @RequestMapping(value = "/society_delete")
    @ResponseBody
    public String baseinfo_delete(String country_code){
        Boolean judge=null;
        try{
            judge=SocietyService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
